<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		changeConfigAttr,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				code: "",
				InstCacheKey: "fin-js-hourWageStatistice-inst-cache",
				virtualGroupCacheKey: "fin-js-hourWageStatistice-virtualGroup-cache",
				userCacheKey: "fin-js-hourWageStatistice-user-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			formSubmit() {
				let _that = this
				if (_that.edit) {
					_that.om.formSave("pieceRateWageStatistics/doUpdate", _that.formdata, _that)
				} else {
					_that.om.formSave("pieceRateWageStatistics/doAdd", _that.formdata, _that)
				}
			},
			getDataList(isShow) {
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.om.request(
					'pieceRateWageStatistics/findDataList', {
						year: formdata.year,
						month: formdata.month,
						instId: formdata.instId,
						virtualGroup: formdata.virtualGroup,
						userId: formdata.userId
					}
				).then(res => {
					_that
					_that.subEditorObj['title1'].workList = res.value
					_that.$refs.autoform.changeConfigAttr("title1", "length", res.value.length || 0)
					if (isShow) {
						errorModal(_that, '提示信息', " 加载了" + (res.value.length || 0 )+ "条产品信息")
					}
				})
			},
			verifyVirtualGroup() {
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if (!formdata.instId) {
					errorModal(_that, '提示信息', "请先选择生产部门")
					return false
				}else{
					return true
				}
			},
			verifyUser() {
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if (!formdata.instId) {
					errorModal(_that, '提示信息', "请先选择生产部门")
					return false
				} else {
					return true
				}
			},
			verifyInst() {
				let _that = this
				if (!_that.$refs.autoform.formdata.zposId) {
					errorModal(_that, '提示信息', "请先选择制表人岗位")
					return false
				} else {
					return true
				}
			},
			getInstList(id) {
				let _that = this
				_that.$refs.autoform.changeEditor('title0', 'instId', {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							containBoard: true,
							code: _that.code,
							postId: id
						}),
						method: "post",
						namefield: 'instName',
						searchfield: "q",
						initload: true,
						url: 'tinstitution/findNotBoard1',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			onSelectZpos(item) {
				let _that = this
				_that.getInstList(item.id)
			},
			onSelectYear(item) {
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.getParamListDataList(item.value, formdata.month, formdata.instId)
			},
			onSelectMonth(item) {
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.getParamListDataList(formdata.year, item.referNum, formdata.instId)
			},
			getParamListDataList(year, month, instId, isShow = false) {
				let _that = this
				_that.$refs.autoform.changeEditor("title0","virtualGroup",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
							year: year,
							month: month,
							instId: instId
						}),
						url:"pieceRateWageStatistics/findGroupList",
						namefield: "text",
						initload:true,
						cachename:_that.virtualGroupCacheKey
					}
				})
					
				_that.$refs.autoform.changeEditor("title0", "userId", {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							year: year,
							month: month,
							instId: instId
						}),
						url: "pieceRateWageStatistics/findUserList",
						namefield: "username",
						initload: true,
						cachename: _that.userCacheKey
					}
				})
				_that.getDataList(isShow)
			},
			intLoad(options) {
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.getMonthList(),
					urlUtil.getYearList()
				]
				let fileId = options.fileId
				if (fileId) {
					ajaxArr.push({
						url: "pieceRateWageStatistics/getValue",
						param: {
							fileId: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeConfigAttr("title1", "showLength", true, mainData)
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					changeEditor("topinfo", "month", "data", configSelectData(res[1], "referNum", "dicname"), mainData)
					changeEditor("topinfo", "year", "data", configSelectData(res[2], "text", "value"), mainData)
					if (_that.subEditorObj["title1"]) {
						changeEditor("title1", "dataStr", {
							showAdd: false,
							showDel: false,
							showArrow: false,
							length: 0,
							showLength: true,
							titleTemplate: "<%='产品编码:' + projectCode %>",
							subtitle1Template: "<%= '产品名称:' + projectName %>"
						}, null, mainData)
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value) {
							if (fieldArr.includes(index)) {
								if(index === "virtualGroup"){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "virtualGroupName", res[3].value[index])
									_that.$set(_that.formdata, "virtualGroupName", res[3].value[index])
								}
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value[index])
								_that.$set(_that.formdata, index, res[3].value[index])
							}
						}
						
						_that.$nextTick(function() {
							let formdata= _that.$refs.autoform.formdata
							_that.getParamListDataList(formdata.year,formdata.month,formdata.instId,false)
							_that.$refs.autoform.initSelectIndex("topinfo", "month")
							_that.$refs.autoform.initSelectIndex("topinfo", "year")
							_that.getInstList(_that.$refs.autoform.formdata.zposId)
							_that.getDataList(false)
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.InstCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.instName
							_that.getParamListDataList(_that.$refs.autoform.formdata.year, _that.$refs.autoform.formdata.month, d.id,
								true)
							uni.removeStorage({
								key: _that.InstCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.userCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.clear("title0", "virtualGroup")
							formdata.userId = d.id
							formdata.userName = d.username
							_that.getDataList(true)
							uni.removeStorage({
								key: _that.userCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.virtualGroupCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.$refs.autoform.clear("title0", "userId")
							formdata.virtualGroup = d.text
							formdata.virtualGroupName = d.value
							_that.getDataList(true)
							uni.removeStorage({
								key: _that.virtualGroupCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
